package java复习202408.B_集合相关验证;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class b4_遍历方式验证 {
//    list 集合的遍历方式
    @Test
    public void test01(){
        ArrayList arrayList = new ArrayList();
        arrayList.add("aaa");
        arrayList.add("bbb");
        arrayList.add("ccc");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println("索引方式遍历》 索引：" + i);
            Object o = arrayList.get(i);
            System.out.println(o);
        }

        for (Object o : arrayList) {
            System.out.println("增强for循环 ：" + o);
        }

        arrayList.forEach(item -> System.out.println("foreach： " + item));

        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()){
            Object next = iterator.next();
            System.out.println("迭代器： " + next);
        }

        arrayList.stream().forEach(item -> System.out.println("stream foreach : " + item));

        arrayList.stream().filter(item -> !"bbb".equals(item)).forEach(item -> System.out.println("stream foreach filter : " + item));

    }

//    set集合的遍历方式
    @Test
    public void test02() {
        HashSet hashSet = new HashSet<>();
        hashSet.add("123");
        hashSet.add("124");
        hashSet.add("111111");
        hashSet.add("125");
        System.out.println("集合内容：" + hashSet);
        hashSet.remove("111111");
        System.out.println("remove 集合内容：" + hashSet);

        for (Object o : hashSet) {
            System.out.println("增强for循环： " + o);
        }

        Iterator iterator = hashSet.iterator();
        while (iterator.hasNext()){
            System.out.println("迭代器遍历： " + iterator.next());
        }

        hashSet.forEach(item -> System.out.println("foreach : " + item));

        hashSet.stream().forEach(item -> System.out.println("stream foreach : " + item));

    }

//    MAP 集合的遍历方式
    @Test
    public void test03(){
        HashMap hashMap = new HashMap();
        hashMap.put("a",1);
        hashMap.put("b",2);
        hashMap.put("d" , 5);
        System.out.println("hashmap : " + hashMap);

        for (Object o : hashMap.keySet()) {
            System.out.println("keyset 遍历键取值：key: " + o + " value: " + hashMap.get(o));
        }

        for (Map.Entry<String,Integer> o : (Set<Map.Entry<String,Integer>>)hashMap.entrySet()) {
            System.out.println("key+value方式遍历： " + o + ", key=" + o.getKey() + " value=" + o.getValue());
        }




    }

}
